
package com.trial.area.invitation.controller;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.trial.area.common.controller.BaseController;
import com.trial.area.common.param.SessionUserInfo;
import com.trial.area.invitation.service.InvitationService;
import com.trial.area.weixin.common.WeixinUtils;

/**
 * 邀请
 *
 * @author 朱国军
 * @since 1.0
 * @version 2016年7月31日 朱国军
 */
@Controller
public class InvitationController extends BaseController {
    
    private Logger logger = LoggerFactory.getLogger(InvitationController.class);

    @Autowired
    private InvitationService invitationService;
    
    private final static String INVITATION_PAGE = "/html5/invitation";
    
    @RequestMapping(value = "/check/invitation/toInvit")
    public ModelAndView toInvit() {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        Map<String, String> resultMap = createWechatData(seUser.getUserInfo().getInvitationCode());
        if(seUser==null||seUser.getUserInfo().getInvitationCode()==null){
        	return new ModelAndView("redirect:"+getPath() + "/check/account", resultMap);
        }else{
        	return new ModelAndView(INVITATION_PAGE, resultMap);
        }
    }
    
    private Map<String, String> createWechatData(Integer inviteCode) {
        
        String basePath = getPath();
        String shareLink = basePath + "/check/invitation/register/" + inviteCode;
        String shareImg = basePath + "/images/yaoq.png";
        String shareTitle = "这里有你想要的一切免费精美好货，快加入试用区吧！";
        String shareContent = "这里有你想要的一切免费精美好货，你没看错，一切免费，不信？那看看吧！";
        String shareContentFriends = "这里有你想要的一切免费精美好货，你没看错，一切免费，不信？那看看吧！";

        Map<String, String> signMap = WeixinUtils.jsapiSign(fullPath);
        // 微信公众号ID
        String appId = WeixinUtils.getApiConfig().getAppId();
        
        signMap.put("shareLink", shareLink);
        signMap.put("shareImg", shareImg);
        signMap.put("shareTitle", shareTitle);
        signMap.put("shareContent", shareContent);
        signMap.put("shareContent_friends", shareContentFriends);
        signMap.put("appId", appId);
        return signMap;
    }
    
    @RequestMapping(value = "/check/invitation/register/{invitionCode}")
    public void register(@PathVariable String invitionCode) {
        SessionUserInfo seUser = (SessionUserInfo) session.getAttribute("sessionUserInfo");
        try {
            invitationService.bindingUser(invitionCode, seUser.getUserId());
        } catch (Exception e) {
            logger.error("绑定邀请关系出错", e);
        }
        
        try {
            if (seUser.getSubscribe() == 1) {
                response.sendRedirect(getPath() + "/index");
            } else {
                response.sendRedirect(getPath() + "/views/html5/attention.jsp");
            }
        } catch (Exception e) {
            logger.error("绑定邀请关系，跳转到首页出错", e);
        }
    }
    
}
